package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 制令单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-04
 */
@Service
public class SysOrderServiceImpl implements ISysOrderService 
{
    @Autowired
    private SysOrderMapper sysOrderMapper;

    /**
     * 查询制令单
     * 
     * @param id 制令单主键
     * @return 制令单
     */
    @Override
    public SysOrder selectSysOrderById(Long id)
    {
        return sysOrderMapper.selectSysOrderById(id);
    }

    /**
     * 查询制令单列表
     * 
     * @param sysOrder 制令单
     * @return 制令单
     */
    @Override
    public List<SysOrder> selectSysOrderList(SysOrder sysOrder)
    {
        return sysOrderMapper.selectSysOrderList(sysOrder);
    }

    /**
     * 新增制令单
     * 
     * @param sysOrder 制令单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysOrder(SysOrder sysOrder)
    {
        sysOrder.setCreateTime(DateUtils.getNowDate());
        int rows = sysOrderMapper.insertSysOrder(sysOrder);
        insertSysDevice(sysOrder);
        return rows;
    }

    /**
     * 修改制令单
     * 
     * @param sysOrder 制令单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysOrder(SysOrder sysOrder)
    {
        sysOrder.setUpdateTime(DateUtils.getNowDate());
        sysOrderMapper.deleteSysDeviceById(sysOrder.getId());
        insertSysDevice(sysOrder);
        return sysOrderMapper.updateSysOrder(sysOrder);
    }

    /**
     * 批量删除制令单
     * 
     * @param ids 需要删除的制令单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysOrderByIds(String ids)
    {
        sysOrderMapper.deleteSysDeviceByIds(Convert.toStrArray(ids));
        return sysOrderMapper.deleteSysOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除制令单信息
     * 
     * @param id 制令单主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderById(Long id)
    {
        sysOrderMapper.deleteSysDeviceById(id);
        return sysOrderMapper.deleteSysOrderById(id);
    }

    /**
     * 新增产线信息
     * 
     * @param sysOrder 制令单对象
     */
    public void insertSysDevice(SysOrder sysOrder)
    {
        List<SysDevice> sysDeviceList = sysOrder.getSysDeviceList();
        Long id = sysOrder.getId();
        if (StringUtils.isNotNull(sysDeviceList))
        {
            List<SysDevice> list = new ArrayList<SysDevice>();
            for (SysDevice sysDevice : sysDeviceList)
            {
                sysDevice.setId(id);
                list.add(sysDevice);
            }
            if (list.size() > 0)
            {
                sysOrderMapper.batchSysDevice(list);
            }
        }
    }
}
